/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entities.Customers;
import entities.Roles;
import java.util.List;
import javax.ejb.Stateless;
import javax.management.relation.Role;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateless
public class CustomersFacade extends AbstractFacade<Customers> implements CustomersFacadeLocal {
    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomersFacade() {
        super(Customers.class);
    }

    public String checkLogin(String username, String password) {
        String result = "";
        List<Customers> Listcu = null;

        Query q = em.createNamedQuery("Customers.login");
        q.setParameter("custID", username);
        q.setParameter("password", password);
        Listcu = q.getResultList();

        int roleid = 0;
        if (Listcu.size() != 0) {
            for (int i = 0; i < Listcu.size(); i++) {
                roleid = Listcu.get(i).getRoles().getRoleId();
                if (roleid == 1) {
                    result = "admin";
                    break;
                } else if (roleid == 2) {
                    result = "customers";
                    break;
                } else if (roleid == 3) {
                    result = "banned";
                    break;
                }
            }
        } else {
            result = "";
        }
        return result;
    }

    public Boolean insertCus(String username, String password, String fname, String lname, String email, String phone, String address, int roleid) {
        Roles objrole = em.find(Roles.class, roleid);
        Customers objcus = new Customers(username, password, fname, lname, email, phone, address, objrole);
        em.persist(objcus);
        return true;
    }

    public List showByUser(String username) {
        Query r = em.createNamedQuery("Customers.findByCustID");
        r.setParameter("custID", username);
        return r.getResultList();
    }

    public Boolean updateCus(String username, String password, String fname, String lname, String email, String phone, String address) {
        Customers C = em.find(Customers.class, username);
        C.setPassword(password);
        C.setFname(fname);
        C.setLname(lname);
        C.setEmail(email);
        C.setPhone(phone);
        C.setAddress(address);
        return true;
    }

    public List showAllCustomer() {
        Query r = em.createNamedQuery("Customers.findAll");
        return r.getResultList();
    }

    public Boolean deleteCus(String username) {
        Customers C = em.find(Customers.class, username);
        em.remove(C);
        return null;
    }

    public List searchCust(String username) {
        Query r = em.createNamedQuery("Customers.findCustID");
        r.setParameter("custID", "%" + username + "%");
        return r.getResultList();
    }

    public List showEditCus(String username) {
        Query r = em.createNamedQuery("Customers.findAll");
        return r.getResultList();
    }

    public List searchDuplicate(String username) {
        Query r = em.createNamedQuery("Customers.findCustID");
        r.setParameter("custID", username);
        return r.getResultList();
    }

    public void removeMuti(String username) {
        em.remove(username);
    }

    public Boolean updateCusAdmin(String username, String password, String fname, String lname, String email, String phone, String address, int roleid) {
        Roles objrole = em.find(Roles.class, roleid);
        Customers C = em.find(Customers.class, username);
        C.setPassword(password);
        C.setFname(fname);
        C.setLname(lname);
        C.setEmail(email);
        C.setPhone(phone);
        C.setAddress(address);
        C.setRoles(objrole);
        return true;
    }

    public Boolean changePass(String username, String password) {
        Customers C = em.find(Customers.class, username);
        C.setPassword(password);
        return true;
    }

        public List<Customers> showByUserReport(String username, int objRole) {
        Query r = em.createNamedQuery("Customers.findReportCust");
        r.setParameter("custID","%" + username + "%");
        r.setParameter("roleId", objRole);
        return r.getResultList();
    }
}
